Crate tree_magic_mini

source
Expand description

tree_magic_mini is a Rust crate that determines the MIME type a given file or byte stream.

This is a fork of the tree_magic crate by Allison Hancock. It includes the following changes:

  • Updated dependencies.
  • Reduced copying and memory allocation, for a slight increase in speed and decrease in memory use.
  • Reduced API surface. Some previously public APIs are now internal.
  • Removed the optional cli feature and tmagic binary.

§About tree_magic

tree_magic is designed to be more efficient and to have less false positives compared to the old approach used by libmagic, or old-fashioned file extension comparisons.

Instead, this loads all known MIME types into a tree based on subclasses. Then, instead of checking against every file type, tree_magic will traverse down the tree and only check the files that make sense to check.

§Features

  • Very fast perfomance (~150ns to check one file against one type, between 5,000ns and 100,000ns to find a MIME type.)
  • Check if a file is a certain type.
  • Handles aliases (ex: application/zip vs application/x-zip-compressed)
  • Can delegate different file types to different “checkers”, reducing false positives by choosing a different method of attack.

§Licensing and the MIME database

By default, tree_magic_mini will attempt to load the shared MIME info database from the standard locations at runtime.

If you won’t have the database files available, or would like to include them in your binary for simplicity, you can optionally embed the database information if you enable the tree_magic_db feature.

As the magic database files themselves are licensed under the GPL, you must make sure your project uses a compatible license if you enable this behaviour.

§Example

// Load a GIF file
let input: &[u8] = include_bytes!("../tests/image/gif");

// Find the MIME type of the GIF
let result = tree_magic_mini::from_u8(input);
assert_eq!(result, "image/gif");

// Check if the MIME and the file are a match
let result = tree_magic_mini::match_u8("image/gif", input);
assert_eq!(result, true);

Functions§

  • Gets the MIME type of a file.
  • Gets the MIME type of a file.
  • Gets the type of a file from a byte stream.
  • Check if the given file matches the given MIME type.
  • Check if the file at the given path matches the given MIME type.
  • Checks if the given bytestream matches the given MIME type.